package promotioncalc

import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*

class PromotionEmployeeController {

    PromotionEmployeeService promotionEmployeeService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

	private int count=0

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond promotionEmployeeService.list(params), model:[promotionEmployeeCount: promotionEmployeeService.count()]
    }

    def show(Long id) {
        respond promotionEmployeeService.get(id)
    }

    def create() {
        respond new PromotionEmployee(params)
    }

    def save(PromotionEmployee promotionEmployee) {
        if (promotionEmployee == null) {
            notFound()
            return
        }

        try {
            promotionEmployeeService.save(promotionEmployee)
        } catch (ValidationException e) {
            respond promotionEmployee.errors, view:'create'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'promotionEmployee.label', default: 'PromotionEmployee'), promotionEmployee.id])
                redirect promotionEmployee
            }
            '*' { respond promotionEmployee, [status: CREATED] }
        }
    }

    def edit(Long id) {
        respond promotionEmployeeService.get(id)
    }

    def update(PromotionEmployee promotionEmployee) {
        if (promotionEmployee == null) {
            notFound()
            return
        }

        try {
            promotionEmployeeService.save(promotionEmployee)
        } catch (ValidationException e) {
            respond promotionEmployee.errors, view:'edit'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'promotionEmployee.label', default: 'PromotionEmployee'), promotionEmployee.id])
                redirect promotionEmployee
            }
            '*'{ respond promotionEmployee, [status: OK] }
        }
    }

    def delete(Long id) {
        if (id == null) {
            notFound()
            return
        }
        promotionEmployeeService.delete(id)

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'promotionEmployee.label', default: 'PromotionEmployee'), id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'promotionEmployee.label', default: 'PromotionEmployee'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }

    def calc(){
		// todo 中文控制台就乱码??
		// println "第:"+(++count)
        log.info("num:"+(++count))
        render( view: 'calc')
    }
}
